package com.hegongda.service.impl;

import com.hegongda.bean.*;
import com.hegongda.service.OrderService;
import com.hegongda.dao.OrdersDao;
import com.hegongda.servlet.Constr;
import com.hegongda.utils.BeanFactory;
import com.hegongda.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrdersDao ordersDao = BeanFactory.newInstance(OrdersDao.class);
    @Override
    public void addOrders(Cart cart, User user,String oid) {

        Orders orders = new Orders();
        String uuid = oid;
        //订单主键
        orders.setOid(uuid);
        //设置总金额
        orders.setTotal( cart.getTotal() );
        //设置下单时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        orders.setOrdertime(sdf.format( new Date() ));
        //订单状态,未付款
        orders.setState(Constr.ORDER_WEIFUKUAN);
        //设置用户主键
        orders.setUid(user.getUid());

        //数据转化,购物项CartItem转换成订单项OrderItem(中间表)
        /*
         * OrderItem订单项数据
         *   购买数量 购物项中数量
         *   商品小计 购物项中小计
         *   商品主键  CartItem.product.pid
         *   订单主键 uuid
         */
        Collection<CartItem>  cartItems = cart.getItemMap().values();
        //创建集合,保存订单项对象
        List<OrderItem> orderItemList = new ArrayList<OrderItem>();
        //遍历集合,取出购物项对象
        for(CartItem cartItem : cartItems){
            //创建订单项对象
            OrderItem orderItem = new OrderItem();
            //设置订单主键
            orderItem.setOid(uuid);
            //设置商品主键
            orderItem.setPid( cartItem.getProduct().getPid() );
            //设置数量
            orderItem.setCount( cartItem.getCount() );
            //设置小计
            orderItem.setSubTotal(cartItem.getSubTotal());
            orderItemList.add(orderItem);
        }
        Connection con = null;
        try {
            con = JdbcUtils.getConnection();
            //开启事务
            con.setAutoCommit(false);
            ordersDao.submitOrder(orders);
            for (OrderItem orderItem : orderItemList) {
                ordersDao.submitOrderItem(orderItem);
            }
            //提交事务
            con.commit();
        }catch (Exception ex){
            ex.printStackTrace();
            //回滚事务
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            if(con!=null) {
                JdbcUtils.close();
            }
        }


    }

    @Override
    public PageBean<Orders> getAllOrder(String uid,int pageSize, int pagenum) {
        long ordersCount = 0;
        List<Orders> allOrder = null;
        try {
            ordersCount = ordersDao.getOrdersCount(uid);
            allOrder = ordersDao.getAllOrder(uid, (pagenum - 1) * pageSize, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageBean<Orders> pb = new PageBean<>();
        pb.setTotalCount(ordersCount);
        pb.setCurrentPage(pagenum);
        pb.setTotalPage((int) Math.ceil(ordersCount * 1.0 / pageSize));
        pb.setPageSize(pageSize);
        pb.setList(allOrder);

        return pb;
    }

    @Override
    public Orders getOneOrder(String oid) {
        Orders oneOrder = null;
        try {
            oneOrder = ordersDao.getOneOrder(oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return oneOrder;
    }

    @Override
    public void updateOrderState(String oid,int state) {
        try {
            ordersDao.updateOrderState(oid,state);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void updateOrder(String name, String address, String telephone, String oid) {
        try {
            ordersDao.updateOrder(name,address,telephone,oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int delOrder(String oid) {
        int i = 0;
        try {
            i = ordersDao.delOrder(oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
